<?php

/**
 * 锁工厂类
 */
namespace app\common\com\disLock;

define('DISLOCK_ROOT', dirname(__FILE__));

class DisLockFactory
{

	/**
	 * 获取一个消息队列对象
	 *
     * @author mayy
     * @date 2017-10-20
     * @param string $type
     * @param array $config
	 * @return DisLock
	 */
	public static function getInstance($type = '', array $config = []) {
		static $return = null;
		if($return instanceof DisLock) {
			return $return;
		}

        $dislock_config    = config('extra.dislock_config');
        $lock_type         = $dislock_config['type'];
        $lock_config       = $dislock_config['config'];

        $type = empty($type)   ? $lock_type   : $type;
        $cfg  = empty($config) ? $lock_config : $config;

        require_once (DISLOCK_ROOT . '/interface/' . $type . '.php');
        $return = new $type($cfg);
        if(!$return instanceof DisLock) {
            exception('dislock_interface_invalid');
        }
        return $return;
	}

}